package com.vcourse.service.admin.controller;


import com.vcourse.commons.result.R;
import com.vcourse.service.admin.entity.Permission;
import com.vcourse.service.admin.service.PermissionService;
import com.vcourse.service.admin.service.RolePermissionService;
import com.vcourse.service.admin.vo.PermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author Zengsh123
 * @since 2020-06-15
 */
@Api("后台菜单管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;
    @Resource
    private RolePermissionService rolePermissionService;

    @ApiOperation("获取全部菜单")
    @GetMapping("getAll")
    public R indexAllPermission() {
        List<PermissionVo> list =  permissionService.queryAllMenu();
        return R.ok().data(list);
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        boolean flag = permissionService.removeChildById(id);
        if (flag) {
            return R.ok().message("删除菜单成功");
        }
        return R.error().message("删除菜单失败");
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public R doAssign(String roleId,String[] permissionId) {
        rolePermissionService.saveRolePermissionRelationShip(roleId, permissionId);
        return R.ok().message("设置权限成功");
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public R toAssign(@PathVariable String roleId) {
        List<PermissionVo> list = permissionService.selectAllMenu(roleId);
        return R.ok().data(list);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public R save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return R.ok();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public R updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return R.ok();
    }
}

